package com.taxi.driver.data.order.local;

import android.text.TextUtils;

import com.qianxx.utils.SP;
import com.taxi.driver.data.entity.CanGrapOrdersEntity;
import com.taxi.driver.data.entity.CancelDesEntity;
import com.taxi.driver.data.entity.ComplainResultEntity;
import com.taxi.driver.data.entity.OrderCostEntity;
import com.taxi.driver.data.entity.OrderEntity;
import com.taxi.driver.data.entity.OrderHomeStatusEntity;
import com.taxi.driver.data.entity.OrderSummaryEntity;
import com.taxi.driver.data.entity.WaitFare;
import com.taxi.driver.data.entity.WxpayInfo;
import com.taxi.driver.data.order.OrderSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import rx.Observable;

/**
 * 功能描述：
 */
public class OrderLocalSource implements OrderSource {

    private static final String KEY_IGNORING_ORDER = "IGNORING_ORDER";
    // key－订单编号、value－订单详情
    private Map<String, OrderEntity> mOrders;
    private List<String> mIgnoreList;

    @Inject
    SP mSP;

    @Inject
    public OrderLocalSource() {
        mOrders = new HashMap<>();
    }

    @Override
    public void cacheOrder(OrderEntity entity) {
        if (entity == null || TextUtils.isEmpty(entity.uuid)) return;
        mOrders.put(entity.uuid, entity); //缓存订单数据
    }

    @Override
    public Observable<OrderHomeStatusEntity> reqHomeStatus() {
        return null;
    }

    @Override
    public Observable<List<OrderSummaryEntity>> reqOrderList(int nowPage) {
        return null;
    }

    @Override
    public Observable<OrderEntity> reqGrab(String orderUuid) {
        return null;
    }

    @Override
    public Observable<OrderEntity> reqOrderDetail(String orderUuid) {
        return Observable.just(mOrders.get(orderUuid));
    }

    @Override
    public Observable<OrderEntity> reqOrderDetail(String orderUuid, boolean onlyFromRemote) {
        if (onlyFromRemote) return Observable.empty(); //返回null
        return Observable.just(mOrders.get(orderUuid)); //从本地缓存取
    }

    @Override
    public Observable<String> reqPickUpPas(String orderUuid, String uploadText) {
        return null;
    }

    @Override
    public Observable<String> reqDepart(String orderUuid, String uploadText) {
        return null;
    }

    @Override
    public Observable<String> reqGetOn(String orderUuid, double waitMile, String uploadText) {
        return null;
    }

    @Override
    public Observable<String> reqArrive(String orderUuid, double tripDistance, String destAddress, String destDetailAddress, String destCity, double destLng, double destLat, String uploadText) {
        return null;
    }

    @Override
    public Observable<String> reqUpdateFare(String orderUuid, double actualFare) {
        return null;
    }

    @Override
    public Observable<String> receiveCash(String orderUuid, String uploadText) {
        return null;
    }

    @Override
    public Observable<String> reqCancelOrder(String orderUuid, String cancelMsg, String uploadText) {
        return null;
    }

    @Override
    public Observable<String> reqComplainOrder(String orderUuid, String contents, String remark) {
        return null;
    }

    @Override
    public Observable<String> rushFare(String orderUuid) {
        return null;
    }

    @Override
    public Observable<ComplainResultEntity> isComplain(String orderUuid) {
        return null;
    }

    @Override
    public Observable<CancelDesEntity> cancelDescription(String orderUuid) {
        return null;
    }

    @Override
    public Observable<WxpayInfo> payByWechat(String orderUuid, String spbillCreateIp) {
        return null;
    }

    @Override
    public Observable<String> payByAlipay(String orderUuid) {
        return null;
    }

    @Override
    public Observable<String> payByBalance(String orderUuid) {
        return null;
    }

    @Override
    public Observable<OrderCostEntity> reqFareItems(String orderUuid) {
        return null;
    }

    @Override
    public Observable<OrderCostEntity> getRealtimeFare(String orderUuid) {
        return null;
    }

    @Override
    public Observable<OrderCostEntity> orderFare(String orderUuid) {
        return null;
    }

    @Override
    public Observable<String> confirmFare(String orderUuid, Integer highwayFare,
                                          Integer roadBridgeFare, Integer otherFare, String uploadText) {
        return null;
    }

    @Override
    public Observable<WaitFare> waitFare(String orderUuid) {
        return null;
    }

    @Override
    public Observable<List<OrderSummaryEntity>> getAppointmentList() {
        return null;
    }

    @Override
    public Observable<CanGrapOrdersEntity> canGrapOrders(String areaCode) {
        return null;
    }

    @Override
    public Observable<List<OrderSummaryEntity>> getOrderPool(String areaCode) {
        return null;
    }

    @Override
    public void ignoringOrder(String orderUuid) {
        if (orderUuid == null) {
            return;
        }
        if (mIgnoreList == null) {
            mIgnoreList = mSP.getList(KEY_IGNORING_ORDER, String.class);
        }
        if (mIgnoreList == null) {
            mIgnoreList = new ArrayList<>();
        }
        for (int i = 0; i < mIgnoreList.size(); i++) {
            if (mIgnoreList.get(i).equals(orderUuid)) {
                return;
            }
        }
        mIgnoreList.add(0, orderUuid);
        if (mIgnoreList.size() > 50) {
            mIgnoreList.remove(50);
        }
        mSP.putList(KEY_IGNORING_ORDER, mIgnoreList);
    }

    @Override
    public Observable<List<String>> ignoreList() {
        mIgnoreList = mSP.getList(KEY_IGNORING_ORDER, String.class);
        if (mIgnoreList != null) {
            return Observable.just(mIgnoreList);
        }
        return Observable.empty();
    }
}
